<template>
    <h3 style="text-align: :center;">投资项目修改</h3>
    <table class="table-bordered" border="1">
        <tr>
            <td>投资名称</td>
            <td>
                <input type="text" v-model="I_Name" />
            </td>
        </tr>
        <tr>
            <td>投资总额度</td>
            <td>
                <input type="text" v-model="I_Budget" />
            </td>
        </tr>
        <tr>
            <td style="text-align: :center;" colspan="2">
                <input type="button" value="修改" @click="SaveData" class="btn btn-success" />
            </td>
        </tr>
    </table>
</template>
<script setup lang="ts">
import { ref, onMounted } from "vue";
import axios from "axios";
import { useRoute, useRouter } from 'vue-router'

//路由取值
const route = useRoute();
const Iid = route.params.Iid;

//定义变量
const I_Name = ref('');
const I_Budget = ref();

//生命周期钩子
onMounted(() => {
    axios.get('https://localhost:44305//api/Invests/SearchInvestNumber?Iid=' + Iid)
        .then(res => {
            I_Name.value = res.data.I_Name;
            I_Budget.value = res.data.I_Budget;
        })
        .catch(error => {
            console.log(error);
        })
})

//修改方法
const SaveData = () => {
    //判断非空
    if (!I_Name.value) {
        alert('投资名称不能为空');
        return;
    }
    if (!I_Budget.value) {
        alert('投资金额不能为空');
        return;
    }

    if (I_Budget.value < 0 || I_Budget.value > 100000000) {
        alert('投资金额需大于0且小于1亿元');
        return;
    }

    axios.put("https://localhost:44305//api/Invests/EditInvestInfo",
        {
            I_Name: I_Name.value,
            I_Budget: I_Budget.value,
            I_Id: Iid
        })
        .then(res => {
            if (res.data > 0) {
                alert('投资项目修改成功！');
                InvestShow();
            }
            else {
                alert('投资项目修改失败！');
            }
        })
        .catch(error => {
            console.log(error);
        })
}



//修改通过路由跳转页面
const router = useRouter();
const InvestShow = () => {
    router.push('/ShowInvestPage')
}

</script>